package com.javaIO.chapter3;


import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

public class StreamPrinter {
    InputStream theInput;

    public static void main1(String[] args) {
        StreamPrinter sr = new StreamPrinter(System.in);
        sr.print();
    }

    public StreamPrinter(InputStream in) {
        theInput = in;
    }


    public void print() {
        try {
            while (true) {
                int datum = theInput.read();
                if (datum == -1) break;
                System.out.println(datum);
            }
        } catch (IOException e) {
            System.err.println("Couldn't read from System.in !");
        }
    }


    public static void test() {

        try {
            byte[] b = new byte[10];
            int offset = 0;
            while (offset < b.length) {
                int bytesRead = System.in.read(b, offset, b.length - offset);
                System.out.println(bytesRead);
                if (bytesRead == -1) break; // end of stream
                offset += bytesRead;
            }
        } catch (IOException e) {
            System.err.println("Couldn't read from   System.in!");
        }
    }


    public static void test1() {
        try {
            byte[] b = new byte[100];
            int offset = 0;
            while (offset < b.length) {
                int a = System.in.available();
                System.out.println("a = " + a);
                int bytesRead = System.in.read(b, offset, a);
                if (bytesRead == -1) break; // end of stream
                offset += bytesRead;
            }
        } catch (IOException e) {
            System.err.println("Couldn't read from System.in!");
        }

    }


    public static void main(String[] args) {
        test1();
    }


}

